<template>
  <div class="caigouruku-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :data="stockList"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序" type="index" width="50" />
      <el-table-column
        align="left"
        label="存货编码"
        prop="invcode"
        width="290"
      />
      <el-table-column
        align="left"
        label="存货名称"
        prop="invname"
        width="290"
      />
      <el-table-column
        align="left"
        label="规格型号"
        prop="invspec"
        width="260"
      />
      <el-table-column
        align="left"
        label="仓库名称"
        prop="whName"
        width="260"
      />
      <el-table-column align="left" label="批次" prop="invbatch" width="260" />
      <el-table-column align="center" label="原始数" width="110">
        <template #default="scope">
          <p style="margin: 0px; font-weight: 600; color: #3300ff">
            {{ scope.row.qty }} {{ scope.row.measName }}
          </p>
        </template>
      </el-table-column>
      <el-table-column align="center" label="上架数" width="110">
        <template #default="scope">
          <p style="margin: 0px; font-weight: 600; color: #8b1a1a">
            {{ scope.row.kyQty }} {{ scope.row.measName }}
          </p>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Mes数" prop="mesQty" width="110">
        <template #default="scope">
          <p style="margin: 0px; font-weight: 600; color: #9932cc">
            {{ scope.row.mesQty }} {{ scope.row.measName }}
          </p>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
  </div>
</template>

<script setup>
  import { Search } from '@element-plus/icons-vue'
  import { getBarcodePageApi } from '@/api/kc/barcode_record'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDialog from './modules/queryDialog.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp })
  const stockList = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 15,
    total: 0,
  })

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
    getStockList()
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
    getStockList()
  }
  //#endregion

  const queryDialogRef = ref()

  const getStockList = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await getBarcodePageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        stockList.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => {
    queryDialogRef.value.openDialog()
  }

  const queryCallback = (value) => {
    queryForm.value = value
    getStockList()
  }

  onMounted(() => getStockList())
</script>
